package cn.duli.xianer.controller;


import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.WantGoods;
import cn.duli.xianer.service.WantGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wantGoods")
public class WantGoodsController {

    @Resource
    private WantGoodsService wantGoodsService;

    /**
     * 新增求购商品
     * @param wantGoods
     */
    @PostMapping("/save")
    @RequiresRoles("business")
    public MyResult save(WantGoods wantGoods) {
        int wgId = wantGoodsService.save(wantGoods);
        return new MyResult(200, "新增求购商品成功",null,null,wgId,null);
    }

    /**
     * 修改求购商品信息
     * @param wantGoods
     */
    @RequestMapping("/update")
    @RequiresRoles("business")
    public MyResult update(WantGoods wantGoods) {
        wantGoodsService.update(wantGoods);
        return new MyResult(200, "修改求购商品信息成功",null,null,null,null);
    }

    /**
     * 根据id查找此求购商品基本信息
     * @param wgId
     * @return
     */
    @GetMapping("/findById/{wgId}")
    public MyResult findById(@PathVariable("wgId") Integer wgId) {
        WantGoods wantGoods = wantGoodsService.findById(wgId);
        return new MyResult(200, "根据id查找此求购商品基本信息成功",null,null,wantGoods,null);
    }

    /**
     * 修改求购商品浏览量
     * @param wantGoods
     * @return
     */
    @RequestMapping("/updateViewNum")
    public MyResult updateViewNum(WantGoods wantGoods) {
        WantGoods wantGood = wantGoodsService.findById(wantGoods.getWgId());
        wantGoods.setWgViewNum(wantGood.getWgViewNum() + 1);
        wantGoodsService.updateViewNum(wantGoods);
        return new MyResult(200, "修改求购商品浏览量",null,null,null,null);
    }

    /**
     * 根据id删除此求购商品（wg_status=0）
     *
     * @param wgId
     */
    @RequestMapping("/deleteById/{wgId}")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult deleteById(@PathVariable("wgId") Integer wgId) {
        wantGoodsService.deleteById(wgId);
        return new MyResult(200, "根据id删除此求购商品成功",null,null,null,null);
    }


    /**
     * 根据id取消删除的求购商品(回收站回收某个求购商品)（wg_status=1）
     *
     * @param wgId
     */
    @RequestMapping("/cancelDeleteById/{wgId}")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult cancelDeleteById(@PathVariable("wgId") Integer wgId) {
        wantGoodsService.cancelDeleteById(wgId);
        return new MyResult(200, "根据id取消删除的求购商品成功",null,null,null,null);
    }


    /**
     * 根据id下架此求购商品（g_mode=0）
     *
     * @param wgId
     */
    @RequestMapping("/shelveById/{wgId}")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult shelveById(@PathVariable("wgId") Integer wgId) {
        wantGoodsService.shelveById(wgId);
        return new MyResult(200, "根据id下架此求购商品成功",null,null,null,null);
    }


    /**
     * 根据id取消被下架的求购商品（上架）
     *
     * @param wgId
     */
    @RequestMapping("/cancelShelveById/{wgId}")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult cancelShelveById(@PathVariable("wgId") Integer wgId) {
        wantGoodsService.cancelShelveById(wgId);
        return new MyResult(200, "根据id取消被下架的求购商品成功",null,null,null,null);
    }


    /**
     * 根据用户id查询该用户下架的求购商品列表
     * @param wgUid
     * @return
     */
    @GetMapping("/findShelveByUserId")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult findShelveByUserId(@RequestParam("wgUid") Integer wgUid,
                                       @RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findShelveByUserId(wgUid));
        return new MyResult(200, "根据用户id查询该用户下架的求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 根据用户id查询该用户审核中的求购商品列表
     * @param wgUid
     * @return
     */
    @GetMapping("/findVerifyIngByUid")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult findVerifyIngByUid(@RequestParam("wgUid") Integer wgUid,
                                       @RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findVerifyIngByUid(wgUid));
        return new MyResult(200, "根据用户id查询该用户审核中的求购商品列表",null,null,null,pageInfo);
    }


    /**
     * 根据用户id查询该用户审核失败的求购商品列表
     * @param wgUid
     * @return
     */
    @GetMapping("/findDieVerifyByUid")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business"})
    public MyResult findDieVerifyByUid(@RequestParam("wgUid") Integer wgUid,
                                       @RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findDieVerifyByUid(wgUid));
        return new MyResult(200, "根据用户id查询该用户审核失败的求购商品列表",null,null,null,pageInfo);
    }

    /**
     * 分页查询所有审核中的求购商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllVerifyIng")
    @RequiresRoles("admin")
    public MyResult findAllVerifyIng(@RequestParam(defaultValue = "1") int pageNo,
                                     @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findAllVerifyIng());
        return new MyResult(200, "分页查询所有审核中的求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 分页查询所有审核失败的求购商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findDieVerify")
    @RequiresRoles("admin")
    public MyResult findDieVerify(@RequestParam(defaultValue = "1") int pageNo,
                                         @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findDieVerify());
        return new MyResult(200, "分页查询所有审核失败的求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 分页查询所有正常的求购商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findSellIng")
    public MyResult findSellIng(@RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findSellIng());
        return new MyResult(200, "分页查询所有正常的求购商品列表成功",null,null,null,pageInfo);
    }


    /**
     * 分页查询所有下架了的求购商品列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findShelved")
    @RequiresRoles("admin")
    public MyResult findShelved(@RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findShelved());
        return new MyResult(200, "分页查询所有下架了的求购商品列表成功",null,null,null,pageInfo);
    }


    /**
     * 根据求购商品id修改此求购商品的审核状态
     * @param wantGoods
     */
    @RequestMapping("/updateMode")
    @RequiresRoles("admin")
    public MyResult updateMode(WantGoods wantGoods) {
        wantGoodsService.updateMode(wantGoods);
        return new MyResult(200, "根据求购商品id修改此求购商品的审核状态成功",null,null,null,null);
    }

    /**
     * 根据用户id查询此用户发布的求购商品列表
     * @param wgUid
     * @return
     */
    @GetMapping("/findByUserId")
    @RequiresRoles(logical = Logical.OR, value = {"admin", "business", "user"})
    public MyResult findByUserId(@RequestParam("wgUid") Integer wgUid,
                                 @RequestParam(defaultValue = "1") int pageNo,
                                 @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findByUserId(wgUid));
        return new MyResult(200, "根据用户id查询此用户发布的求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 分页查询所有求购商品
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                                   @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.findAll());
        return new MyResult(200, "分页查询所有求购商品列表成功",null,null,null,pageInfo);
    }


    /**
     * 分页根据商品名称模糊查询求购商品列表
     * @param wgName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryLikeByGoodName")
    public MyResult queryLikeByGoodName(@RequestParam(value="wgName") String wgName,
                                               @RequestParam(defaultValue = "1") int pageNo,
                                               @RequestParam(defaultValue = "6") int pageSize ) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.queryLikeByGoodName(wgName));
        return new MyResult(200, "分页根据商品名称模糊查询求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 分页根据类别名模糊查询求购商品列表
     * @param tName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryLikeByTypeName")
    public MyResult queryLikeByTypeName(@RequestParam(value="tName") String tName,
                                               @RequestParam(defaultValue = "1") int pageNo,
                                               @RequestParam(defaultValue = "6") int pageSize ) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.queryLikeByTypeName(tName));
        return new MyResult(200, "分页根据类别名模糊查询求购商品列表成功",null,null,null,pageInfo);
    }


    /**
     * 分页根据发布地点查询求购商品列表
     * @param wgAddress
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryLikeByAddr")
    public MyResult queryLikeByAddr(@RequestParam(value="wgAddress") String wgAddress,
                                           @RequestParam(defaultValue = "1") int pageNo,
                                           @RequestParam(defaultValue = "6") int pageSize ) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.queryLikeByAddr(wgAddress));
        return new MyResult(200, "分页根据发布地点查询求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 分页根据时间区间动态查询发布的求购商品列表
     * @param startTime
     * @param endTime
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryByTimeInterval")
    public MyResult queryByTimeInterval(String startTime,String endTime,
                                               @RequestParam(defaultValue = "1",required = false) int pageNo,
                                               @RequestParam(defaultValue = "6",required = false) int pageSize ){
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.queryByTimeInterval(startTime, endTime));
        return new MyResult(200, "分页根据时间区间动态查询发布的求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 分页根据二级类别id查询该类别下的求购商品列表
     * @param tId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryByTypeTwoLevelId/{tId}")
    public MyResult queryByTypeTwoLevelId(@PathVariable("tId") Integer tId,
                                          @RequestParam(defaultValue = "1",required = false) int pageNo,
                                          @RequestParam(defaultValue = "6",required = false) int pageSize ) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.queryByTypeTwoLevelId(tId));
        return new MyResult(200, "分页根据二级类别id查询该类别下的求购商品列表成功",null,null,null,pageInfo);

    }

    /**
     * 根据时间区间、价格区间、类别动态分页查询求购商品列表
     * @param startTime
     * @param endTime
     * @param lowPrice
     * @param topPrice
     * @param wgTid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryDynamic")
    public MyResult queryDynamic(@RequestParam(defaultValue = "") String startTime,
                                 @RequestParam(defaultValue = "") String endTime,
                                 @RequestParam(defaultValue = "") String lowPrice,
                                 @RequestParam(defaultValue = "") String topPrice,
                                 @RequestParam(defaultValue = "") String wgTid,
                                 @RequestParam(defaultValue = "1",required = false) int pageNo,
                                 @RequestParam(defaultValue = "6",required = false) int pageSize){
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<WantGoods> pageInfo = new PageInfo<>(wantGoodsService.queryDynamic(startTime, endTime, lowPrice,
                topPrice, wgTid));
        return new MyResult(200, "动态分页查询求购商品列表成功",null,null,null,pageInfo);
    }

    /**
     * 查找求购商品总数
     * @return
     */
    @RequestMapping("/findWantGoodsCount")
    @RequiresRoles("admin")
    public MyResult findWantGoodsCount() {
        int count = wantGoodsService.findWantGoodsCount();
        return new MyResult(200, "查找求购商品总数成功",null,null,count,null);
    }
}
